
#include "main.h"
#include "cmsis_os.h"

void SystemClock_Config(void);
static void MX_GPIO_Init(void);
extern void VL53L0X_Init(void);
extern uint16_t VL53L0X_GetValue(void);
/* VL53L0X GPIO 引脚配置 */
static I2C_GPIO_TypeDef VL53L0X_I2C_GPIO = {
    .SCL_GPIO_Port = VL53L0X_SCL_GPIO_PORT,
    .SDA_GPIO_Port = VL53L0X_SDA_GPIO_PORT,
    .SCL_GPIO_Pin = VL53L0X_SCL_GPIO_PIN,
    .SDA_GPIO_Pin = VL53L0X_SDA_GPIO_PIN,
};
Customize_I2C_TypeDef *VL53L0X_Soft_I2C;

int main(void)
{
    // 设置中断分组
    HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4);

    HAL_Init();
    SystemClock_Config();
    MX_GPIO_Init();

    DebugUart_Init();           // 串口初始化
    UserLed_GPIO_Init();        // LED初始化
    USER_LED_ON();              // LED灯亮
		HOTLED_GPIO_Init();					// 加热灯初始化
		WATLED_GPIO_Init();					// 水泵指示灯初始化
		WATHOT_GPIO_Init();					// 水加热指示灯
		BUZZER_GPIO_Init();         // 蜂鸣器初始化
    DHT11_GPIO_Init();          // DHT11初始化
    Delay_TIM_Init();           // 定时器初始化
    HAL_Delay(100);
    DS18B20_Init();                                    // DS18B20初始化
    VL53L0X_Soft_I2C = MY_I2C_Init(&VL53L0X_I2C_GPIO); // VL52L0X  IIC 初始化
    VL53L0X_Soft_I2C->I2C_GPIO_Init();                 // IIC  GPIO 初始化
    VL53L0X_Init();                                    //VL53L0X 初始化
    ScreenUart_Init();                                  // 串口屏 通信串口初始化
    Hall_GPIO_Init(); // 霍尔传感器初始化
    Fan_PWM_GPIO_Init();//风扇相关 GPIO初始化
    TB6612_AIN_GPIO_Init();//TB6612 AIN 初始化
		TDS_Init();//TDS传感器初始化
    Fan_PWM_TIM_Init();//风扇初PWM始化
    Set_Fan_Speed(FAN_SPEED_LEVEL_7);
    Set_Fan_State(FAN_STATE_OFF);


    osKernelInitialize();
    Create_APP_Task();
    osKernelStart();

    while (1)
    {
    }
}

/**
 * @brief System Clock Configuration
 * @retval None
 */
void SystemClock_Config(void)
{
    RCC_OscInitTypeDef RCC_OscInitStruct = {0};
    RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

    /** Initializes the RCC Oscillators according to the specified parameters
     * in the RCC_OscInitTypeDef structure.
     */
    RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
    RCC_OscInitStruct.HSEState = RCC_HSE_ON;
    RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
    RCC_OscInitStruct.HSIState = RCC_HSI_ON;
    RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
    RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
    RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
    if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
    {
        Error_Handler();
    }

    /** Initializes the CPU, AHB and APB buses clocks
     */
    RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
    RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
    RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
    RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
    RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

    if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
    {
        Error_Handler();
    }
}

/**
 * @brief GPIO Initialization Function
 * @param None
 * @retval None
 */
static void MX_GPIO_Init(void)
{
    /* USER CODE BEGIN MX_GPIO_Init_1 */

    /* USER CODE END MX_GPIO_Init_1 */

    /* GPIO Ports Clock Enable */
    __HAL_RCC_GPIOD_CLK_ENABLE();
    __HAL_RCC_GPIOA_CLK_ENABLE();

    /* USER CODE BEGIN MX_GPIO_Init_2 */

    /* USER CODE END MX_GPIO_Init_2 */
}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/* USER CODE BEGIN Header_StartDefaultTask */
/**
 * @brief  Function implementing the defaultTask thread.
 * @param  argument: Not used
 * @retval None
 */
/* USER CODE END Header_StartDefaultTask */

/**
 * @brief  Period elapsed callback in non blocking mode
 * @note   This function is called  when TIM1 interrupt took place, inside
 * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
 * a global variable "uwTick" used as application time base.
 * @param  htim : TIM handle
 * @retval None
 */
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
    /* USER CODE BEGIN Callback 0 */

    /* USER CODE END Callback 0 */
    if (htim->Instance == TIM1)
    {
        HAL_IncTick();
    }
    /* USER CODE BEGIN Callback 1 */

    /* USER CODE END Callback 1 */
}

/**
 * @brief  This function is executed in case of error occurrence.
 * @retval None
 */
void Error_Handler(void)
{
    /* USER CODE BEGIN Error_Handler_Debug */
    /* User can add his own implementation to report the HAL error return state */
    __disable_irq();
    while (1)
    {
    }
    /* USER CODE END Error_Handler_Debug */
}

#ifdef USE_FULL_ASSERT
/**
 * @brief  Reports the name of the source file and the source line number
 *         where the assert_param error has occurred.
 * @param  file: pointer to the source file name
 * @param  line: assert_param error line source number
 * @retval None
 */
void assert_failed(uint8_t *file, uint32_t line)
{
    /* USER CODE BEGIN 6 */
    /* User can add his own implementation to report the file name and line number,
       ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
    /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
